package com.dreamfire.asset
{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.utils.Dictionary;

	public class AssetManager
	{
//		[Embed(source="/../embeds/master.swf", symbol="master")]
//		public var Master:Class;
		
//		[Embed(source="/../embeds/map.png")]
//		public var Map:Class;
		
		private var _bitmapManager:BitmapManager = null;
		private var _soundManager:SoundManager = null;
		
		private var _reference:Object;
		private var _loaderDic:Dictionary;
		
		private static var _instance:AssetManager;
		
		public static function get instance():AssetManager
		{
			if(_instance == null)
				_instance = new AssetManager();
			return _instance;
		}
		
		public function AssetManager()
		{
			_bitmapManager = BitmapManager.instance;
			_soundManager = SoundManager.instance;
			
			_reference = {};
			_loaderDic = new Dictionary();
		}
		
		public function getAsset( url:String ):*
		{
			return _reference[url];				
		}
		
		public function destroyLoaderListener( loader:LoaderInfo, onComplete:Function = null, onProgress:Function = null, onIoError:Function = null, onSecError:Function = null ):void
		{
			if( onComplete != null )
				loader.removeEventListener(Event.COMPLETE, onComplete);
			if( onProgress != null )
				loader.removeEventListener(ProgressEvent.PROGRESS, onProgress);
			if( onIoError != null )
				loader.removeEventListener(IOErrorEvent.IO_ERROR, onIoError);
			if( onSecError != null )
				loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecError);
		}
		
		public function load( url:String, onComplete:Function = null, onProgress:Function = null, onIoError:Function = null, onSecError:Function = null ):void
		{
			var loader:Loader = new Loader();
			var loaderCont:LoaderContext = new LoaderContext();
			var req:URLRequest = new URLRequest( url );
			_loaderDic[loader.contentLoaderInfo] = url;
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
			if( onComplete != null )
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			if( onProgress != null )
				loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
			if( onIoError != null )
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
			if( onSecError != null )
				loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecError);
			loader.load( req, loaderCont );
		}
		
		protected function loadComplete( evt:Event ):void
		{
			var loaderInfo:LoaderInfo = evt.target as LoaderInfo;
			var url:String = _loaderDic[loaderInfo];
			_reference[url] = loaderInfo.content;
			_loaderDic[loaderInfo] = null;
			loaderInfo.removeEventListener( Event.COMPLETE, loadComplete );
		}
	}
}